package com.maxima.attendance.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.attendance.daos.HolidayDAO;
import com.maxima.attendance.pojos.Holiday;
import com.opensymphony.xwork2.ActionSupport;

public class HolidaysAction extends ActionSupport implements SessionAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Holiday holiday = new Holiday();
	private Map<String, Object> map;
	private Integer id;
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());
	List<Holiday> holiList = new HolidayDAO().getAllHolidays();
	List<Holiday> holidayList = new ArrayList<Holiday>();

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	public String addHoliday() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date date = null;
		try {
			date = sdf.parse(ServletActionContext.getRequest().getParameter("date"));
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		String description = ServletActionContext.getRequest().getParameter("description");
		for (Holiday holiday : holiList) {
			holidayList.add(new Holiday(holiday));
		}
		for (Holiday holiday : holidayList) {
			String oldDate = sdf.format(holiday.getDate());
			String newDate = sdf.format(date);
			System.out.println("old : " + oldDate + " new : " + newDate);
			if (oldDate.equals(newDate)) {
				return ERROR;
			}
		}
		this.holiday.setDate(date);
		this.holiday.setDescription(description);
		new HolidayDAO().add(holiday);
		logger.info("New Holiday - " + this.holiday.getDescription() + " Id - " + this.holiday.getId() + " - added Successfully");
		return SUCCESS;
	}

	public String selectHoliday() throws Exception {
		map.put(SessionManager.SELECTED_HOLIDAY, new HolidayDAO().getHoliday(this.getId()));
		this.setSession(map);
		return SUCCESS;
	}

	public String updateHoliday() throws Exception {
		HolidayDAO holidayDAO = new HolidayDAO();
		int holidayId = ((Holiday) this.getMap().get(SessionManager.SELECTED_HOLIDAY)).getId();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date date = null;
		try {
			date = sdf.parse(ServletActionContext.getRequest().getParameter("date"));
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		String description = ServletActionContext.getRequest().getParameter("description");
		for (Holiday holiday : holiList) {
			holidayList.add(new Holiday(holiday));
		}
		for (Holiday holiday : holidayList) {
			String oldDate = sdf.format(holiday.getDate());
			String newDate = sdf.format(date);
			System.out.println("old : " + oldDate + " new : " + newDate);
			if (!holiday.getId().equals(holidayId)) {
				if (oldDate.equals(newDate)) {
					return ERROR;
				}
			}
		}
		Holiday holiday = holidayDAO.getHoliday(holidayId);
		holiday.setDate(date);
		holiday.setDescription(ServletActionContext.getRequest().getParameter("description"));
		holidayDAO.update(holidayId, holiday);
		map.put(SessionManager.SELECTED_HOLIDAY, null);
		this.setSession(map);
		logger.info("Holiday - " + holiday.getDescription() + " Id - " + holidayId + " - updated Successfully");
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		this.map = map;
	}

	public String clearSelectedHoliday() {
		map.put(SessionManager.SELECTED_HOLIDAY, null);
		return SUCCESS;
	}
}
